﻿@model IList<RolesEntity>
	<div class="layui-card">
		<div class="layui-form layui-card-header layuiadmin-card-header-auto" style="padding-top:0px;padding-bottom:0px;">
			<div class="layui-form-item">
				<div class="layui-inline" style="margin-right: 0;">
					<label class="layui-form-label" style="padding:0px;padding-right: 10px;width:150px;">
						<select id="dropRoles">
							<option value="">所属角色</option>
							<option value="0">超级管理员</option>
							@foreach (var role in Model)
							{
								<option value="@role.RoleID">@role.RoleName</option>
							}
						</select>
					</label>
					<div class="layui-input-inline">
						<select id="dropSearchName">
							<option value="AdminID">管理员ID</option>
							<option value="AdminName" selected="selected">管理员名</option>
							<option value="ParentAdminName">上级管理员</option>
							<option value="UserName">前台用户名</option>
						</select>
					</div>
					<div class="layui-input-inline" style="margin: 0px;">
						<input type="text" class="layui-input" id="txtSearchKeyword" name="txtSearchKeyword" placeholder="请输入搜索关键字" autocomplete="off" onkeypress="javascript:return DefaultButton(event, 'btnSearch')">
					</div>
				</div>
				<div class="layui-inline">
					<button id="btnSearch" type="button" class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="btnSearch">
						<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
					</button>
				</div>
			</div>
		</div>

		<div class="layui-card-body">
			<div class="layui-tab layui-tab-brief" lay-filter="tabList">
				<ul class="layui-tab-title">
					<li lay-id="0" class="layui-this">全部</li>
					<li lay-id="1">一个月未修改密码的</li>
					<li lay-id="2">24小时登录的</li>
					<li lay-id="3">锁定的</li>
					<li lay-id="4">允许多人登录的</li>
				</ul>
				<div class="layui-tab-content">
					<table id="tblList" lay-filter="tblList"></table>
				</div>
			</div>
			<input type="hidden" id="hidTab" value="0" />
			<script type="text/html" id="toolbarHead">
				<div class="layui-form-item" style="margin: auto;">
					<div class="layui-inline">
						<button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="btnDeleteMulti">批量删除</button>
						<button class="layui-btn layuiadmin-btn-list" onclick="layer_show('添加管理员','/Admin/Administrator/AdminEdit')">添加</button>
					</div>
				</div>
			</script>
			<script type="text/html" id="statusText">
				{{#  if(d.IsLock){ }}
				<button class="layui-btn layui-btn-danger layui-btn-xs">已锁定</button>
				{{#  } else{ }}
				<button class="layui-btn layui-btn-normal layui-btn-xs">正常</button>
				{{#  } }}
			</script>
			<script type="text/html" id="statusText1">
				{{#  if(d.IsMultiLogin){ }}
				<button class="layui-btn layui-btn-normal layui-btn-xs">允许</button>
				{{#  } else{ }}
				<button class="layui-btn layui-btn-danger layui-btn-xs">不允许</button>
				{{#  } }}
			</script>
			<script type="text/html" id="tblBarCol">
				{{#  if(d.IsLock){ }}
				<a class="layui-btn layui-btn-xs" lay-event="set"><i class="layui-icon layui-icon-reduce-circle"></i>启用</a>
				{{#  } else{ }}
				<a class="layui-btn layui-btn-xs" lay-event="set"><i class="layui-icon layui-icon-password"></i>锁定</a>
				{{#  } }}
				<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
				<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
			</script>
		</div>
	</div>
@Html.AntiForgeryToken()
@section scripts {
	<script type="text/javascript">
		layui.use(['form', 'table', 'element'], function () {
			var table = layui.table;
			var form = layui.form;
			var element = layui.element;

			//获取hash来切换选项卡，假设当前地址的hash为lay-id对应的值
			var layid = location.hash.replace(/^#tabList=/, '');
			if (layid != null && layid != "") {
				element.tabChange('tabList', layid);
				$("#hidTab").val(layid);
			}
			//监听Tab切换，以改变地址hash值
			element.on('tab(tabList)', function () {
				location.href = location.pathname + "#tabList=" + this.getAttribute('lay-id');
				window.location.reload();
			});

			//加载表格
			table.render({
				elem: '#tblList'
				, url: '/Admin/Administrator/GetAdminList'
				, toolbar: '#toolbarHead'
				, where: GetSearchParam()
				, request: {
					pageName: 'PageNum' //页码的参数名称，默认：page
					, limitName: 'PageSize' //每页数据量的参数名，默认：limit
				}
				, cols: [[
					{ type: "checkbox", width: 30, fixed: "left", align: 'center' },
					{ field: 'AdminName', width: 120, title: '登录名', align: 'center' },
					{ field: 'ParentAdminName', width: 120, title: '上级管理员', align: 'center' },
					{ field: 'UserName', width: 100, title: '前台用户名', align: 'center' },
					{ field: 'RoleNames', minWidth: 200, title: '角色', align: 'center' },
					{ field: 'IsMultiLogin', width: 100, title: '多人登录', align: 'center', templet: '#statusText1' },
					{ field: 'LoginTime', width: 170, title: '最后登录时间', align: 'center', templet: function (d) { return (d.LoginTime == null ? "" : (formatDateTime(d.LoginTime) == "9999-12-31 23:59:59") ? "" : formatDateTime(d.LoginTime)); } },
					{ field: 'LoginIP', width: 150, title: '最后登录IP', align: 'center' },
					{ field: 'ModifyPasswordTime', width: 170, title: '上次修改密码时间', align: 'center', templet: function (d) { return (d.ModifyPasswordTime == null ? "" : (formatDateTime(d.ModifyPasswordTime) == "9999-12-31 23:59:59") ? "" : formatDateTime(d.ModifyPasswordTime)); } },
					{ field: 'LoginTimes', width: 100, title: '登录次数', align: 'center' },
					{ field: 'IsLock', width: 100, title: '管理员状态', align: 'center', templet: '#statusText' },
					{ field: 'AdminID', width: 230, title: '操作', fixed: "right", align: "center", templet: '#tblBarCol' }
				]]
				, parseData: function (res) { //res 即为原始返回的数据
					return {
						"code": 0, //解析接口状态
						"msg": "", //解析提示文本
						"count": res.RecordTotal, //解析数据长度
						"data": res.RowList //解析数据列表
					};
				}
				, limits: [10, 15, 20, 25, 50, 100]
				, limit: 15
				, page: true
			});
			//监听表格操作列
			table.on('tool(tblList)', function (obj) {
				if (obj.event === 'set') {
					SetAdminStatus(obj.data.AdminID);
					return false;
				} else if (obj.event === 'edit') {
					layer_show('编辑管理员', '/Admin/Administrator/AdminEdit?id=' + obj.data.AdminID);
					return false;
				} else if (obj.event === 'del') {
					DeleteSingle(obj.data.AdminID, "/Admin/Administrator/DelAdmin");
					return false;
				}
			});
			//监听搜索操作
			form.on('submit(btnSearch)', function (data) {
				reloadTable();
				return false;
			});
			//监听批量删除
			form.on('submit(btnDeleteMulti)', function (data) {
				var checkStatus = table.checkStatus('tblList');
				var data = checkStatus.data;
				var ids = "";
				for (j = 0, len = data.length; j < len; j++) {
					if (j < len - 1) {
						ids += data[j].AdminID + ',';
					} else {
						ids += data[j].AdminID;
					}
				}
				DeleteMulti(ids, '/Admin/Administrator/DelAdminMulti');
				return false;
			});

			//表格重载
			function reloadTable(tableID = 'tblList') {
				table.reload(tableID, {
					page: {
						curr: 1
					}
					, where: GetSearchParam()
				}, 'data');
			};
			function GetSearchParam() {
				var roleID = $("#dropRoles").val();
				var SearchName = $("#dropSearchName").val();
				var SearchKeyword = $("#txtSearchKeyword").val();
				var TabStatus = $("#hidTab").val();
				var objParam = { "roleID": roleID, "SearchName": SearchName, "SearchKeyword": SearchKeyword, "TabStatus": TabStatus };
				return objParam;
			};
		});
		function SetAdminStatus(adminID) {
			$.ajax({
				type: "POST",
				url: "/Admin/Administrator/SetAdminStatus",
				data: { "id": adminID },
				headers: {
					"X-CSRF-TOKEN-JXWebHost": $("input[name='AntiforgeryFieldname']").val()
				},
				error: function (data, status, e) {
					layer.alert('操作失败!');
				},
				success: function (data) {
					if (data.Result == "ok") {
						layer.alert('操作成功!');
						setTimeout('window.location.reload()', 1000);
					}
					else {
						layer.alert(data.Result);
					}
				}
			})
		}
	</script>
}